package com.power.doc.utils;

import java.io.*;
import java.util.Properties;

/**
 * 文件获取工具类
 */
public class PropertiesUtils {
    /*
     * 根据json文件名称获取json配置文件数据
     *
     * @param fileName json文件名称前缀，如果在resource下直接写文件名，如果有路径，请在前面添加路径如："com/xxx/abc"
     */
    /*public static JSONObject getJsonResource(String fileName){
        fileName+=".json";
        ClassLoader classLoader = getClassLoader();

        Enumeration<URL> resources;
        JSONObject jsonObject = new JSONObject();
        try {
            resources = classLoader.getResources(fileName);
        } catch (IOException e) {
            //LOGGER.warn("getJsonResource fail {}", fileName, e);
            return jsonObject;
        }

        while (resources.hasMoreElements()) {
            URL url = resources.nextElement();
            try {
                String json = Resources.toString(url, Charsets.UTF_8);
                jsonObject.putAll(JSON.parseObject(json)); // 有多个的时候，后面的覆盖前面的
            } catch (IOException e) {
                //LOGGER.warn("addJsonFile fail url:{}", url, e);
            }
        }
        return jsonObject;
    }*/

    private static ClassLoader getClassLoader() {
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        if (classLoader != null) {
            return classLoader;
        }
        return PropertiesUtils.class.getClassLoader();
    }

    /**
     * 私有构造方法，防止工具类被new
     */
    private PropertiesUtils() {
        throw new IllegalAccessError();
    }


    public static String readJsonData(String pactFile) throws IOException {
        // 读取文件数据
        //System.out.println("读取文件数据util");

        StringBuffer strbuffer = new StringBuffer();
        File myFile = new File(pactFile);//"D:"+File.separatorChar+"DStores.json"
        if (!myFile.exists()) {
            System.err.println("Can't Find " + pactFile);
            return null;
        }
        try {
            FileInputStream fis = new FileInputStream(pactFile);
            InputStreamReader inputStreamReader = new InputStreamReader(fis, "UTF-8");
            BufferedReader in  = new BufferedReader(inputStreamReader);
            String str;
            while ((str = in.readLine()) != null) {
                strbuffer.append(str);  //new String(str,"UTF-8")
            }
            in.close();
        } catch (IOException e) {
            e.getStackTrace();
        }
        //System.out.println("读取文件结束util");
        return strbuffer.toString();
    }


    public static String readFile(String fileName)
    {
        String ret = null;
        try
        {
            ret = readJarFile(fileName);
        }
        catch (IOException e)
        {
            System.err.println(fileName + "文件读取异常："+e.getMessage());
            //BaseLog.getDailyLog().error(fileName + "文件读取异常：", e);
            //throw new ServiceException(PublicErrorCodeEnum.ERR_SERVER_ERROR.getCode(), e.getMessage());
        }
        return ret;
    }

    /**
     *  读取jar包中的资源文件
     * @param fileName 文件名
     * @return 文件内容
     * @throws IOException 读取错误
     */
    public static String readJarFile(String fileName) throws IOException
    {
        BufferedReader in = new BufferedReader(
                new InputStreamReader(PropertiesUtils.class.getClassLoader().getResourceAsStream(fileName), "UTF-8"));
        StringBuilder buffer = new StringBuilder();
        String line;
        while ((line = in.readLine()) != null)
        {
            buffer.append(line);
        }
        return buffer.toString();
    }

    public static Properties getProperties(String fileName) throws IOException{
        Properties props = new Properties();
        props.load(PropertiesUtils.class.getClassLoader()
                .getResourceAsStream(fileName));
        return props;
    }

}
